package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.DTO.AddOrderParamDTO;
import com.woniuxy.entity.DTO.OrderDTO;
import com.woniuxy.entity.DTO.SubmitOrderDTO;
import com.woniuxy.entity.OrderStatus;
import com.woniuxy.entity.PaymentMethod;
import com.woniuxy.service.OrderService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @author ：admin
 * @version ：1.0
 * @date ：Created in 2025/5/13 10:11
 * @description ：TODO
 * @modified_by ：
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * <p>从前端页面获取当前页数 pageNum 、每页显示记录数 pageSize 以及查询参数 param ，此处 param 应当为 null 。</p>
     * <p>根据分页参数，查询所有订单信息。</p>
     * @param param 包含 pageNum 、 pageSize 、 param 的参数， param 的泛型为订单类 OrderDTO 。
     * @return 包含查询结果及分页信息的 PageInfo 对象。
     */
    @PostMapping("/queryOrderList")
    public ResponseData<?> queryOrderList(@RequestBody PageParam<OrderDTO> param) {
        PageInfo<OrderDTO> pageInfo = orderService.queryOrderListByPage(param);
        if (pageInfo == null) {
            return ResponseData.fail(ResponseEnum.ERROR_PARAM); // 如果 pageInfo 为空，则代表前端传递的参数不正确
        }
        return ResponseData.success(pageInfo);
    }

    /**
     * <p>查询所有订单状态。</p>
     * @return 订单状态列表
     */
    @GetMapping("/queryStatusList")
    public ResponseData<?> queryStatusList() {
        List<OrderStatus> orderStatusList = orderService.queryStatusList();
        return ResponseData.success(orderStatusList);
    }

    /**
     * <p>查询所有支付方式。</p>
     * @return 支付方式列表
     */
    @GetMapping("/queryPaymentMethodList")
    public ResponseData<?> queryPaymentMethodList() {
        List<PaymentMethod> paymentMethodList = orderService.queryPaymentMethodList();
        return ResponseData.success(paymentMethodList);
    }

    /**
     * <p>根据条件分页查询订单信息。</p>
     * @param param 分页查询的条件参数
     * @return 包含查询结果和分页信息的 pageInfo 对象
     */
    @PostMapping("/queryOrderByCondition")
    public ResponseData<?> queryOrderByCondition(@RequestBody PageParam<OrderDTO> param) {
        PageInfo<OrderDTO> pageInfo = orderService.queryOrderByCondition(param);
        if (pageInfo == null) {
            return ResponseData.fail(ResponseEnum.ERROR_PARAM); // 如果 pageInfo 为空，则代表前端传递的参数不正确
        }
        return ResponseData.success(pageInfo);
    }

    /**
     * <p>根据订单编号查询订单详情信息。</p>
     * @param orderId 订单编号
     * @return 订单详情
     */
    @GetMapping("/queryOrderItem/{orderId}")
    public ResponseData<?> queryOrderItem(@PathVariable("orderId") String orderId) {
        OrderDTO orderDTO = orderService.queryOrderItemByOrderId(orderId);
        if (orderDTO == null) {
            return ResponseData.fail(ResponseEnum.ERROR_PARAM); // 如果 orderDTO 为空，则代表前端传递的参数不正确
        }
        return ResponseData.success(orderDTO);
    }

    /**
     * <p>用户点击“去结算”按钮，根据购物车中被选中的商品生成订单。</p>
     *
     * @param addOrderParamDTOs 被选中的商品
     * @return 是否插入成功
     */
    @PostMapping("/addOrder")
    public ResponseData<?> addOrderFromCart(@RequestBody List<AddOrderParamDTO> addOrderParamDTOs) {
        if (addOrderParamDTOs == null || addOrderParamDTOs.isEmpty()) {
            return ResponseData.fail(ResponseEnum.ERROR_PARAM);
        }
        try {
            orderService.addOrderFromCart(addOrderParamDTOs);
        } catch (InterruptedException | ParseException e) {
            return ResponseData.fail(ResponseEnum.ADD_ORDER_FROM_CART_ERROR);
        }
        return ResponseData.success();
    }

    /**
     * <p>立即购买对应的添加订单</p>
     * @param goodsId
     */
    @GetMapping("/addOrderForCommonGoods")
    public ResponseData<?> addOrderForCommonGoods(@RequestParam("goodsId") Integer goodsId) {
        String orderId;
        try {
            orderId = orderService.addOrderForCommonGoods(goodsId);
        } catch (ParseException | InterruptedException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success(orderId);
    }

    /**
     * <p>提交订单。</p>
     * @param submitOrderDTO
     * @return
     */
    @PostMapping("/submitOrder")
    public ResponseData<?> submitOrder(@RequestBody SubmitOrderDTO submitOrderDTO) {
        String orderId;
        try {
            orderId = orderService.submitOrder(submitOrderDTO);
        } catch (ParseException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success(orderId);
    }

    /**
     * <p>支付</p>
     * @param orderId
     * @return
     */
    @GetMapping("/pay")
    public ResponseData<?> pay(@RequestParam("orderId") String orderId) {
        try {
            return orderService.pay(orderId);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
